package com.GamersCom.games;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.GamersCom.db.Game;
import com.GamersCom.db.Genre;
import com.GamersCom.db.Platform;
import com.GamersCom.model.GameMgr;
import com.GamersCom.model.GenreMgr;
import com.GamersCom.model.MgrFactory;
import com.GamersCom.model.PlatformMgr;

/**
 * Servlet implementation class GameDetail
 */
public class GameDetail extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String nextView = "gameDetail.jsp";
		MgrFactory factory = (MgrFactory) this.getServletContext()
				.getAttribute("mgrFactory");
		PlatformMgr pfMgr = factory.getPlatformMgr();
		GenreMgr genreMgr = factory.getGenreMgr();
		GameMgr gameMgr = factory.getGameMgr();

		// retrieve the parameter
		String id = request.getParameter("id");

		if (id == null)
		{
			nextView = "result.jsp";
			request.setAttribute("error", "404 Page Not Found");
		}
		else
		{
			int gid = Integer.parseInt(id);
			try
			{
				List<Platform> pfList = pfMgr.getAll();
				List<Genre> genreList = genreMgr.getAll();
				Game game = gameMgr.getGameById(gid);
				request.setAttribute("pfList", pfList);
				request.setAttribute("genreList", genreList);
				request.setAttribute("game", game);
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		request.getRequestDispatcher(nextView).forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
